package com.iaz.tech.tools.tasks.schedule;

import com.iaz.tech.tools.common.utils.UtilTools;
import com.iaz.tech.tools.property.IPropertyAble;
import com.iaz.tech.tools.tasks.ability.IScheduleAble;
import com.iaz.tech.tools.tasks.ability.IScheduleCursorAble; 

import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 

/**
 * 抽象类， Schedule Node 节点
 * 用于
 * planStartTime, PlanEndTime 为-1 的时候，没有限制
 */
@Data
public abstract class ISchedule implements IScheduleAble, IScheduleCursorAble {

    /**
     * 日志
     */
    private final static Logger logger = (Logger) LoggerFactory.getLogger( ISchedule.class );

    private static long __iNNER_SCHEDULE_SERAILNO = 0L;
    private final static String __SYN_SCHEDULE_SERAILNO = "_N_LOCK_FOR_SCHEDULE_SERIAL";

    private long scheduleID;
    private long planStartTime = -1;
    private long planEndTime = -1;
    private long startTime = -1;
    private long endTime = -1;
    private long cycle = -1;

    protected ISchedule() {
        try {
            this.scheduleID = ISchedule.GenerateScheduleCode();
        } catch(Exception e) {
            logger.error("实例对象异常， 错误信息：" + e.getMessage());
        }
    }

    /**
     * 获得任务的ID号，在系统重是唯一的
     * @return 当前时间戳并合并000
     */
    public static long GenerateScheduleCode() {
        long result = -1L;
        synchronized( __SYN_SCHEDULE_SERAILNO ) {
            if( __iNNER_SCHEDULE_SERAILNO >= 9999) {
                __iNNER_SCHEDULE_SERAILNO= 0L;
            }
            __iNNER_SCHEDULE_SERAILNO++;
            result = UtilTools.getCurrentTime() * 10000 + __iNNER_SCHEDULE_SERAILNO;
        }
        return result;
    }

    @Override
    public long planStartTime() {
        return this.getPlanStartTime();
    }

    @Override
    public long planEndTime() {
        return this.getPlanEndTime();
    }

    @Override
    public long cycleTimes() {
        return this.getCycle();
    }

    @Override
    public void registerStatTime(long _time) {
        this.startTime = _time;
    }

    @Override
    public void registerEndTime(long _time) {
        this.endTime = _time;
    }

    /**
     * 返回计划任务的唯一ID
     * @return
     */
    @Override
    public long scheduleIdentiyNo() {
        return this.scheduleID;
    }

    /**
     * 满足执行计划，根据计划开始时间和结束时间，以及Cycle 时间进行处理
     * @return
     */
    @Override
    public Boolean confirm() {
        boolean result = false;
        try {
            long current_time = UtilTools.getCurrentTime();
            if ((this.getPlanStartTime() != -1) && (current_time < this.getPlanStartTime())){
                return result;
            }

            if ((this.getPlanEndTime() != -1) && (current_time > this.getPlanEndTime())){
                return result;
            }
            result = true;
        } catch( Exception e) {
            logger.error("执行confirm出现错误：" + e.getMessage());
        }
        return result;
    }
    /**
     * 执行任务
     */
    @Override
    public IPropertyAble execute(IPropertyAble properties) {
        logger.error("抽象类不需要具体的执行execute()");
        return null;
    }
    /**
     * 停止任务
     * @return
     */
    @Override
    public void stop() { 
    }
}
